package com.asia5b.a5mall.middleware.widget;

import android.content.Context;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asia5b.a5mall.middleware.R;


/**
 * Created by zhangyuncai on 2018/9/28.
 * 加减法view
 */
public class PlusView extends LinearLayout implements View.OnClickListener {
    TextView tv_plusview_sub;
    EditText et_plusview_num;
    TextView tv_plusview_add;
    private QtyChangeListener listener;
    private int currentQty = 1;
    private int minQty = 1;//该商品选购最低数量
    private int maxQty = 1;//该商品选购最高数量

    public void setListener(QtyChangeListener listener) {
        this.listener = listener;
    }

    public interface QtyChangeListener {
        void call(int qty);
    }

    public PlusView(Context context) {
        super(context);
        init();
    }

    public PlusView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PlusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = View.inflate(getContext(), R.layout.view_plus, this);
        tv_plusview_sub = view.findViewById(R.id.tv_plusview_sub);
        et_plusview_num = view.findViewById(R.id.et_plusview_num);
        tv_plusview_add = view.findViewById(R.id.tv_plusview_add);
        et_plusview_num.setFocusableInTouchMode(false);
        et_plusview_num.setFocusable(false);
        et_plusview_num.setText("1");

        initEvent();
    }

    public void openEditText() {
        et_plusview_num.setFocusableInTouchMode(true);
        et_plusview_num.setFocusable(true);
    }

    public void closeEditText() {
        et_plusview_num.setFocusable(false);
        et_plusview_num.clearFocus();
    }

    private void initEvent() {
        tv_plusview_sub.setOnClickListener(this);
        tv_plusview_add.setOnClickListener(this);
        checkCurrentQty();
        et_plusview_num.addTextChangedListener(new BaseTextWatcher() {

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
                int newQty = 0;
                String s = "";
                if (charSequence != null) {
                    s = charSequence.toString();
                }
                if (TextUtils.isEmpty(s)) {
                    newQty = 0;
                } else {
                    try {
                        newQty = Integer.parseInt(s);
                    } catch (Exception e) {
                        newQty = 0;
                    }
                }
//                if (newQty != currentQty) {
//                    checkCurrentQty();
//                }

                if (newQty < minQty) {
//                    et_plusview_num.setText(minQty+"");//todo 防止输入数字的位数为0后,又被置于1
                    currentQty=newQty;
                    call();
                } else if (newQty > maxQty) {
                    et_plusview_num.setText(maxQty + "");
                    currentQty = maxQty;
                    call();
                }
            }

        });
//        tv_plusview_sub.setOnLongClickListener(this);
//        tv_plusview_add.setOnLongClickListener(this);
    }

    public void hideKeyboard() {
        String s = et_plusview_num.getText().toString().trim();
        if (TextUtils.isEmpty(s)) {
            currentQty = 0;
        } else {
            try {
                int num = Integer.parseInt(s);
                currentQty = num;
            } catch (Exception e) {
                e.printStackTrace();
                currentQty = 0;
            }
        }
        checkCurrentQty();
    }

    /**
     * 检测数量是否在合理范围内
     */
    private void checkCurrentQty() {


        if (currentQty < minQty) {
            currentQty = minQty;
//            ToastUtil.showToast(String.format(ResString.get(R.string.shopcar_minqty_notice), minQty));
        } else if (currentQty > maxQty) {
            currentQty = maxQty;
        }
        et_plusview_num.setText(currentQty + "");
        call();


        if (currentQty == minQty || currentQty <= 0) {
            tv_plusview_sub.setEnabled(false);
        } else {
            tv_plusview_sub.setEnabled(true);
        }

        if (currentQty >= maxQty) {
            tv_plusview_add.setEnabled(false);
        } else {
            tv_plusview_add.setEnabled(true);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_plusview_sub) {
            currentQty += -1;
            checkCurrentQty();
        } else if (v.getId() == R.id.tv_plusview_add) {
            currentQty += 1;
            checkCurrentQty();
        }
    }


    private void call() {
        if (listener != null) {
            listener.call(currentQty);
        }
    }

    public int getQty() {
        return currentQty;
    }

    public void setQty(int qty) {
        this.currentQty = qty;
        et_plusview_num.setText(qty + "");
        checkCurrentQty();
    }

    public void setMinQty(int minQty) {
        this.minQty = minQty;
        checkCurrentQty();
    }

    public void setMaxQty(int maxQty) {
        this.maxQty = maxQty;
        checkCurrentQty();
    }
}
